package scau.online.Validator;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import scau.cty.entity.Address;

/**
 * 地址验证器
 * Created by Sunlight on 2017/3/28.
 */
public class AddressValidator implements Validator{

    /**
     *该验证器只支持验证Address对象
     * @param aClass
     * @return
     */
    @Override
    public boolean supports(Class<?> aClass) {
        return Address.class.equals(aClass);
    }

    @Override
    public void validate(Object o, Errors errors) {
        Address address = (Address) o;
        ValidationUtils.rejectIfEmpty(errors,"phone",null,"phone is null!");
        ValidationUtils.rejectIfEmpty(errors,"name",null,"name is null!");
        ValidationUtils.rejectIfEmpty(errors,"content",null,"content is null!");
        ValidationUtils.rejectIfEmpty(errors,"postcode",null,"post is null!");
        ValidationUtils.rejectIfEmpty(errors,"defaultChoice",null,"defaultChoice is null!");
        if(!address.getDefaultChoice().equals(1) || !address.getDefaultChoice().equals(0)){
            errors.rejectValue("defaultChoice","defaultChoice is invalid,only can be 1 or 0!");
        }

    }
}
